package aod.android.com.bernay.loginandregister;

import android.content.Intent;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.Set;

import aod.android.com.bernay.R;
import aod.android.com.bernay.base.BernayBaseActivity;
import aod.android.com.bernay.http.BernayApi;
import aod.android.com.bernay.http.BernayCallback;
import aod.android.com.bernay.loginandregister.bean.PhoneCodeBean;
import aod.android.com.bernay.loginandregister.bean.UserDataBean;
import aod.android.com.bernay.util.SharePreUtils;
import aod.android.com.bernay.view.activity.MainActivity;
import chen.lion.hilib.util.GeneralUtils;
import chen.lion.hilib.util.LogUtils;
import chen.lion.hilib.view.bind.HiView;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

public class RegisterAndForgetPwdActivity extends BernayBaseActivity implements View.OnClickListener {
    @HiView(R.id.title_iv_back)
    ImageView title_iv_back;
    @HiView(R.id.resiter_tv)
    TextView resiter_tv;
    @HiView(R.id.title_tv_name)
    TextView title_tv_name;

    @HiView(R.id.resiter_phone_ed)
    EditText resiter_phone_ed;
    private String registerPhoneStr;
    @HiView(R.id.register_code_ed)
    EditText register_code_ed;
    private String registerCodeStr;
    @HiView(R.id.register_pwd_ed)
    EditText register_pwd_ed;
    private String registerPwdStr;
    @HiView(R.id.register_agin_pwd_ed)
    EditText register_agin_pwd_ed;
    private String registerAginPwdStr;
    @HiView(R.id.register_code_tv)
    TextView register_code_tv;

    private String register = "";
    //倒计时的时间
    private TimeCount time;

    @Override
    protected int initLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected void initData() {
        time = new TimeCount(60000, 1000);
        if(getIntent().getStringExtra("register") != null){
            register = getIntent().getStringExtra("register");
            if(register.equals("1")){
                title_tv_name.setText(getString(R.string.login_register));
                resiter_tv.setText(getString(R.string.login_register));
            }else {
                title_tv_name.setText(getString(R.string.login_forget_pwd));
                resiter_tv.setText(getString(R.string.login_affirm_pwd));
            }
        }
        title_iv_back.setOnClickListener(this);
        resiter_tv.setOnClickListener(this);
        register_code_tv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_iv_back:
                finish();
                break;
            case R.id.resiter_tv:
                //注册
                registerPhoneStr = resiter_phone_ed.getText().toString();
                registerPwdStr = register_pwd_ed.getText().toString();
                registerAginPwdStr = register_agin_pwd_ed.getText().toString();
                registerCodeStr = register_code_ed.getText().toString();
                if(TextUtils.isEmpty(registerPhoneStr)){
                    showError("输入手机号为空");
                    return ;
                }
                if(!GeneralUtils.isMobileNO(registerPhoneStr)){
                    showError("输入手机号格式错误");
                    return;
                }
                if(TextUtils.isEmpty(registerCodeStr)){
                    showError("输入验证码为空");
                    return ;
                }
                if (TextUtils.isEmpty(registerPwdStr)||TextUtils.isEmpty(registerAginPwdStr)) {
                    showError("输入密码为空");
                    return ;
                }
                if(!GeneralUtils.isPwdNO(registerPwdStr)||!GeneralUtils.isPwdNO(registerAginPwdStr)){
                    showError("输入密码格式错误");
                    return;
                }
                if(!registerPwdStr.equals(registerAginPwdStr)){
                    showError("两次输入密码不一致");
                    return;
                }
                if(register.equals("1")){
                    //注册
                    goRegiter();
                }else {
                    //找回密码
                    goResetpass();
                }

                break;
            case R.id.register_code_tv:
                registerPhoneStr = resiter_phone_ed.getText().toString();
                if(TextUtils.isEmpty(registerPhoneStr)){
                    showError("请输入手机号为空");
                    return ;
                }
                if(!GeneralUtils.isMobileNO(registerPhoneStr)){
                    showError("输入手机号格式错误");
                    return;
                }
                //获取验证码
                getPhoneCode();
                break;
        }
    }
    private void goResetpass() {
        BernayApi.http().resetpass(registerPhoneStr,registerCodeStr, registerPwdStr,registerAginPwdStr ,new BernayCallback() {
            @Override
            public void onJsonData(String json) {
                Gson gson = new Gson();
                UserDataBean userDataBean =  gson.fromJson(json,UserDataBean.class);
                if(userDataBean.getStatus()==1){
                    goActivity(MainActivity.class);
                    finish();
                }else if(userDataBean.getStatus()==-1){
                    showToast("找回密码失败");
                }else {
                    showToast("找回密码失败");
                }
            }
            @Override
            public void onHttpFailure(IOException e) {
                LogUtils.json(new Gson().toJson(e));
            }
        });
    }

    private void goRegiter() {
        BernayApi.http().register(registerPhoneStr,registerCodeStr, registerPwdStr,registerAginPwdStr ,new BernayCallback() {
            @Override
            public void onJsonData(String json) {
                Gson gson = new Gson();
                UserDataBean userDataBean =  gson.fromJson(json,UserDataBean.class);
                if(userDataBean.getStatus()==1){
                    SharePreUtils.newInstance(RegisterAndForgetPwdActivity.this).updata(userDataBean.getData().get(0).getUserId());
                    JPushInterface.setAlias(mContext, userDataBean.getData().get(0).getUserId(), new TagAliasCallback() {
                        @Override
                        public void gotResult(int i, String s, Set<String> set) {
                            LogUtils.e("gotResult i="+i+",s="+s+",set="+new Gson().toJson(set));
                        }
                    });
                    SharePreUtils.newInstance(RegisterAndForgetPwdActivity.this).updataToken(userDataBean.getData().get(0).getToken());
                    goActivity(MainActivity.class);
                    finish();
                }else if(userDataBean.getStatus()==-1){
                    showToast("注册失败");
                }else {
                    showToast("注册失败");
                }
            }
            @Override
            public void onHttpFailure(IOException e) {
                LogUtils.json(new Gson().toJson(e));
            }
        });


    }

    private void getPhoneCode() {
        BernayApi.http().getPhoneCode(registerPhoneStr,new BernayCallback() {
            @Override
            public void onJsonData(String json) {
                Gson gson = new Gson();
                PhoneCodeBean userDataBean =  gson.fromJson(json,PhoneCodeBean.class);
                if(userDataBean.getStatus()==1){
                    showToast("验证码发送成功");
                    time.start();
                }else if(userDataBean.getStatus()==-1){
                    showToast("登录失败");
                }
            }
            @Override
            public void onHttpFailure(IOException e) {
                LogUtils.json(new Gson().toJson(e));
            }
        });
    }
    /**
     * 发送短信倒计时
     */
    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {// 计时完毕
            register_code_tv.setTextColor(Color.parseColor("#02A48B"));
            register_code_tv.setText("重新发送验证码");
            register_code_tv.setClickable(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程
            register_code_tv.setClickable(false);//防止重复点击
            register_code_tv.setTextColor(Color.parseColor("#CCCCCC"));
            register_code_tv.setText("("+millisUntilFinished / 1000 + "S后)"+"重新获取" );
        }
    }

}
